package edu.scau.cxq.gp.schedule;

import edu.scau.cxq.gp.service.IInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

/**
 * 整理库存定时任务
 */
//@Component
@EnableScheduling
public class InventoryMergeTask implements SchedulingConfigurer {

    private static final String DEFAULT_CRON = "0 0 0 * * ?";
    private String cron = DEFAULT_CRON;

    @Autowired
    private IInventoryService inventoryService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(() -> {
//            logger.info("真正的任务执行逻辑。。。");

            // 整理库存
            inventoryService.sortInventory();
            System.out.println("任务逻辑执行");
        }, triggerContext -> {
            CronTrigger trigger = new CronTrigger(cron);
            return trigger.nextExecutionTime(triggerContext);
        });
    }
    public void setInventoryCron(String cron) {

//        cron = cron.substring(0, cron.length() - 2);
        System.out.println("原来的cron："+this.cron+"更新后的cron："+cron);
        this.cron = cron;
    }
    public String getInventoryCron() {
        return this.cron;
    }
}
